package com.ruoyi.zhjd.exp.modules.laboratorystatistics.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zhjd.exp.modules.laboratorystatistics.entity.LaboratoryStatisticsEntity;
import com.ruoyi.zhjd.exp.modules.laboratorystatistics.mapper.LaboratoryStatisticsMapper;
import com.ruoyi.zhjd.exp.modules.laboratorystatistics.service.LaboratoryStatisticsService;
import com.ruoyi.zhjd.exp.modules.laboratorystatistics.utils.LaboratoryStatisticsEnum;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LaboratoryStatisticsServiceImpl extends ServiceImpl<LaboratoryStatisticsMapper, LaboratoryStatisticsEntity> implements LaboratoryStatisticsService {


    @Override
    public Map<String, Object> datastatistics(Map<String, Object> map) {
         Map<String,Object> list = null;
        if(ObjectUtils.isEmpty(map.get("experimentalType"))){
            throw new ServiceException("参数异常！");
        }
        if(StringUtils.equals(String.valueOf(map.get("experimentalType")),"asphalt")){
             list = baseMapper.datastatistics(map);
        }else{
             list = baseMapper.mashallData(map);
        }

        Map<String,Object> result = new HashMap<>();
        List<Integer> xs = new ArrayList<>();
        List<String> data = new ArrayList<>();
        if(!ObjectUtils.isEmpty(list)){
            for(String k:list.keySet()){
                if(k.contains("type")){
                    data.add(String.valueOf(list.get(k)==null?"":list.get(k)));
                    xs.add(Integer.parseInt(String.valueOf(list.get(k.substring(0,1)+"num")==null?0:list.get(k.substring(0,1)+"num"))));
                }
            }
        }
        result.put("xAxis",xs);
        result.put("data",data);
        return result;
    }


    @Override
    public List<Map<String, Object>> datapies(Map<String, Object> map) {
        List<Map<String, Object>> list = null;
        if(StringUtils.equals(String.valueOf(map.get("experimentalType")),"asphalt")){
            list = baseMapper.datapies(map);
        }else{
            list = baseMapper.mashallPies(map);
        }
        return list;
    }

//    @ZhjdDataFilter
    @Override
    public Map<String, Object> dataQualifiedCol(Map<String, Object> map) {
        Map<String,BigDecimal> list = new HashMap<>();
        if(StringUtils.equals(String.valueOf(map.get("experimentalType")),"asphalt")){
           list = baseMapper.dataQualifiedCol(map);
        }else{
            list = baseMapper.marshallQualifiedCol(map);
        }

        Map<String,Object> result = new HashMap<>();
        List<String> xs = new ArrayList<>();
        List<String> data = new ArrayList<>();
        if(!ObjectUtils.isEmpty(list)){
            for(String k:list.keySet()){
                if(k.contains("type")){
                    xs.add(String.valueOf(list.get(k)==null?"":list.get(k)));
                    data.add(String.valueOf(list.get(k.substring(0,1)+"al")==null?"":list.get(k.substring(0,1)+"al")));
                }

            }
        }
        result.put("xAxis",xs);
        result.put("data",data);
        return result;
    }

//    @ZhjdDataFilter
    @Override
    public List<Map<String, Object>> dataStatisticsByType(Map<String, Object> map) {
        List<Map<String,Object>> list = new ArrayList<>();
        if(ObjectUtils.isEmpty(map.get("category"))){
            throw new ServiceException("参数不全！");
        }

        this.convertParam(map);
        if(ObjectUtils.isEmpty(map.get("table"))){
            throw new ServiceException("参数错误！"+map.get("category"));
        }
        Map<String,Object> paras = new HashMap<>();
        List<Map<String,Object>> piem = getPieByType(map);
        paras.put("pei",piem);
        Map<String,Object> lines = getLineByType(map);

        list.add(paras);
        list.add(lines);
        Map<String,Object> requires = getLineRequiresByType(map);
        list.add(requires);
        if (StringUtils.equals(String.valueOf(map.get("category")), LaboratoryStatisticsEnum.MARSHALL.getCode())) {
            map.put("marshallType",LaboratoryStatisticsEnum.MARSHALL_TYPE.getCode());
            map.put("max",LaboratoryStatisticsEnum.MARSHALL_MAX.getTable());
            map.put("min",LaboratoryStatisticsEnum.MARSHALL_MIN.getTable());
            Map<String,Object> flowrequires = getLineRequiresByType(map);
            list.add(flowrequires);
        }
        return list;
    }

//    @ZhjdDataFilter(tableAlias = "a")
    public List<Map<String,Object>> getPieByType(Map<String,Object> map){
        return baseMapper.getPieByType(map);
    }

    public Map<String,Object> getLineByType(Map<String,Object> map){
        List<Map<String,Object>> result = baseMapper.getLineByType(map);
        Map<String,Object> ret = new HashMap<>();
        List<String> yaxis = new ArrayList<>();
        List<BigDecimal> al = new ArrayList<>();
        List<BigDecimal> noq = new ArrayList<>();
        result.stream().forEach(m -> {
            m.keySet().stream().forEach(s -> {
                if(StringUtils.equals(s,"yaxis")){
                    yaxis.add(String.valueOf(m.get(s)==null?"":m.get(s)));
                }
                if(StringUtils.equals(s,"al")){
                    al.add(new BigDecimal(String.valueOf(m.get("al"))));
                }
                if(StringUtils.equals(s,"noq")){
                    noq.add(new BigDecimal(ObjectUtils.isEmpty(m.get("noq"))?"0":String.valueOf(m.get("noq"))));
                }
            });
        });
        ret.put("lengent",yaxis);
        ret.put("al",al);
        ret.put("noq",noq);
        return ret;
    }
    public Map<String,Object> getLineRequiresByType(Map<String,Object> map){
//        this.convertParam(map);
        Map<String,Object> result = new HashMap<>();
        List<String> yaxis = new ArrayList<>();
        List<BigDecimal> max = new ArrayList<>();
        List<BigDecimal> avager = new ArrayList<>();
        List<BigDecimal> min = new ArrayList<>();
        List<String> name = new ArrayList<>();
        baseMapper.getLineRequiresByType(map).stream().forEach(m -> {
                    yaxis.add(String.valueOf(m.get("end_time")));
                if(!ObjectUtils.isEmpty(map.get("avagerage"))) {
                    avager.add(new BigDecimal(String.valueOf(m.get(map.get("avagerage"))==null?"0":m.get(map.get("avagerage")))));
                }
                if(!ObjectUtils.isEmpty(map.get("min"))) {
                    min.add(new BigDecimal(String.valueOf(m.get(map.get("min")))));
                }
                if(StringUtils.equals(String.valueOf(map.get("category")),LaboratoryStatisticsEnum.PENETRATION.getCode()) || StringUtils.equals(String.valueOf(map.get("category")),LaboratoryStatisticsEnum.MARSHALL.getCode())) {
                    if(!ObjectUtils.isEmpty(map.get("max"))){
                        max.add(new BigDecimal(String.valueOf(m.get(map.get("max")))));
                    }
                }
                if(StringUtils.equals(String.valueOf(map.get("name")),LaboratoryStatisticsEnum.SAMPLE_NAME_TYPE.getCode())) {
                    name.add(String.valueOf(m.get(map.get("name"))));
                }
        });
        result.put("yaxis",yaxis);
        result.put("min",min);
        result.put("averager",avager);
        result.put("max",max);
        result.put("name",name);
        return result;
    }

    private void convertParam(Map<String, Object> map) {
        if(ObjectUtils.isEmpty(map.get("category"))){
            throw new ServiceException("参数不全！");
        }
        if (StringUtils.equals(String.valueOf(map.get("category")), LaboratoryStatisticsEnum.SOFT_POINT.getCode())) {
            map.put("table",LaboratoryStatisticsEnum.SOFT_POINT.getTable());
            map.put("avagerage",LaboratoryStatisticsEnum.SOFT_COLOUMN.getTable());
            map.put("min",LaboratoryStatisticsEnum.SOFT_MIN.getTable());
            map.put("name",LaboratoryStatisticsEnum.SAMPLE_NAME_TYPE.getCode());
        }
        if (StringUtils.equals(String.valueOf(map.get("category")), LaboratoryStatisticsEnum.DUCTILITY.getCode())) {
            map.put("table",LaboratoryStatisticsEnum.DUCTILITY.getTable());
            map.put("avagerage",LaboratoryStatisticsEnum.SOFT_COLOUMN.getTable());
            map.put("min",LaboratoryStatisticsEnum.SOFT_MIN.getTable());
            map.put("name",LaboratoryStatisticsEnum.SAMPLE_NAME_TYPE.getCode());
        }
        if (StringUtils.equals(String.valueOf(map.get("category")), LaboratoryStatisticsEnum.PENETRATION.getCode())) {
            map.put("table",LaboratoryStatisticsEnum.PENETRATION.getTable());
            map.put("avagerage",LaboratoryStatisticsEnum.SOFT_COLOUMN.getTable());
            map.put("min",LaboratoryStatisticsEnum.SOFT_MIN.getTable());
            map.put("max",LaboratoryStatisticsEnum.SOFT_MAX.getTable());
            map.put("name",LaboratoryStatisticsEnum.SAMPLE_NAME_TYPE.getCode());
        }
        if (StringUtils.equals(String.valueOf(map.get("category")), LaboratoryStatisticsEnum.MARSHALL.getCode())) {
            map.put("table",LaboratoryStatisticsEnum.MARSHALL.getTable());
            map.put("name",LaboratoryStatisticsEnum.SAMPLE_NAME_TYPE.getCode());
            if(StringUtils.equals(String.valueOf(map.get("marshallType")),LaboratoryStatisticsEnum.MARSHALL_TYPE.getCode())){
                //流值
                map.put("avagerage",LaboratoryStatisticsEnum.SOFT_COLOUMN.getTable());
                map.put("min",LaboratoryStatisticsEnum.MARSHALL_MIN.getTable());
                map.put("max",LaboratoryStatisticsEnum.MARSHALL_MAX.getTable());
            }else{//稳定值
                map.put("avagerage",LaboratoryStatisticsEnum.MARSHALL_STABILITY_COLUMN.getTable());
                map.put("min",LaboratoryStatisticsEnum.MARSHALL_STABILITY.getTable());
            }
        }
        if (StringUtils.equals(String.valueOf(map.get("category")), LaboratoryStatisticsEnum.FURNACE.getCode())) {
            map.put("table",LaboratoryStatisticsEnum.FURNACE.getTable());
            map.put("avagerage",LaboratoryStatisticsEnum.FURNACE_COLUMN_RATIO.getTable());
            map.put("min",LaboratoryStatisticsEnum.FURNACE_COLUMN_ASPHALT.getTable());
            map.put("name",LaboratoryStatisticsEnum.SAMPLE_NAME_TYPE.getCode());
        }
    }
}
